@extends('admin.layout')
@section('content')
    @if ($message = Session::get('success'))
        <div class="layui-card" >
            <div class="layui-card-header">提示！</div>
            <div class="layui-card-body">
                <p>{{ $message }}</p>
            </div>
        </div>
    @endif
    <div class="layui-card">

        <div class="layui-card-body">
            <form action="{{route('admin.configs.save')}}" method="post"  class="layui-form">
                {{csrf_field()}}
                <textarea id="edit_image" style="display: none;"></textarea>
                <div class="layui-tab" lay-filter="demo" style="">
                    <ul class="layui-tab-title">
                        @foreach($data['tab'] as $key =>$val)
                        <li @if($key == 0)class="layui-this" @endif>{{$val}}</li>
                       @endforeach
                    </ul>
                    <div class="layui-tab-content">


                        @foreach($data['configs'] as $k =>$tab)
                        <div class="layui-tab-item  @if($k == '基本') layui-show @endif " >
                            @foreach($tab as $key =>$val)
                            <div class="layui-form-item">
                                <label class="layui-form-label">{{$val['title']}}： </label>
                                <div class="layui-input-block">
                                    @if($val['type']== 'text')
                                        <input type="text" name="{{$val['name']}}"  value="{{$val['value']}}"  autocomplete="off" class="layui-input">

                                    @elseif($val['type']== 'textarea')
                                        <textarea placeholder="请输入{{$val['title']}}" name="{{$val['name']}}"  class="layui-textarea">{{$val['value']}}</textarea>
                                    @elseif($val['type']== 'switch')
                                        <input type="checkbox" value="1" @if($val['value']==1) checked="" @endif name="{{$val['name']}}" lay-skin="switch" lay-filter="switchTest" lay-text="开启|关闭">
                                    @elseif($val['type']== 'picture')
                                        <button type="button"  class="layui-btn upload-image">上传图片</button>
                                        <div class="layui-upload-list">

                                            <img class="layui-upload-img" src="{{getImagesById($val['value'])}}" style="width: 90px" >
                                            <input type="hidden" name="{{$val['name']}}" value="{{$val['value']}}">
                                        </div>
                                    @endif
                                </div>
                            </div>
                            @endforeach
                        </div>
                        @endforeach

                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">立即提交</button>
                    </div>
                </div>



            </form>
        </div>
    </div>
@endsection
@section('script')
    <style>
        .layui-form-label{width: 120px;}
        .layui-input-block { margin-left: 150px;}
    </style>

    <script>


        layui.use(['layer', 'form','jquery','formSelects','strpy','inputTags','element','laytpl'], function(){
            var layer = layui.layer,
                form =layui.form,
                element=layui.form,
                laytpl = layui.laytpl,
                inputTags = layui.inputTags,
                $  = layui.jquery;

            var _editor = UE.getEditor("edit_image",{
                initialFrameWidth:800,
                initialFrameHeight:300,
                zIndex:19891026,
            });
            _editor.ready(function (){
                _editor.hide();
                _editor.addListener('beforeInsertImage',function(t,arg){
                    console.log(arg);
                    console.log(t);
                    var obj = _editor.queryCommandValue("serverparam");
                    if(obj.type == "upload-image"){
                        if(arg.length>1){
                            layer.msg("最多只能选择1张图片，请重新选择");
                            return false;
                        }
                        if(arg.length>0) {
                            var uploadImageTd = obj.uploadImageItem.parent().find(".layui-upload-list");
                            console.log(uploadImageTd);
                            uploadImageTd.find('img').attr('src',arg[0].src);
                            uploadImageTd.find('input').val(arg[0].image_id);
                            uploadImageTd.show();
                        }else{
                            layer.msg("请先选择图片");
                            return false;
                        }
                    }
                    return true;
                });
            });

            $("body").on("click", ".upload-image", function () {

                //此处进行初始化
                var obj = _editor.queryCommandValue("serverparam");
                obj.type = "upload-image";
                obj.uploadImageItem = $(this);
                var uploadImage = _editor.getDialog("insertimage");
                console.log(obj);
                uploadImage.open();
            });

        });
    </script>

@endsection